/*
 * Copyright (C) 2008
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.googlecode.sca4idea.facet;

import com.intellij.facet.FacetConfiguration;
import com.intellij.facet.ui.FacetEditorTab;
import com.intellij.facet.ui.FacetEditorContext;
import com.intellij.facet.ui.FacetValidatorsManager;
import com.intellij.openapi.util.InvalidDataException;
import com.intellij.openapi.util.WriteExternalException;
import com.intellij.openapi.components.PersistentStateComponent;
import com.intellij.util.xmlb.annotations.Property;
import com.intellij.util.xmlb.annotations.MapAnnotation;
import org.jdom.Element;
import com.googlecode.sca4idea.providers.VanillaSCAProvider;
import com.googlecode.sca4idea.providers.SCAProvider;
import com.googlecode.sca4idea.providers.SCAProviderRegistry;

import java.util.Map;
import java.util.HashMap;

/**
 * Configuration for the SCA Facet.
 */
public class SCAFacetConfiguration implements FacetConfiguration, PersistentStateComponent<SCAFacetConfiguration.SCAFacetState> {
    private static final String PROVIDER_KEY = "sca-provider";

    private SCAProvider selectedProvider = VanillaSCAProvider.INSTANCE;

    public FacetEditorTab[] createEditorTabs(FacetEditorContext facetEditorContext, FacetValidatorsManager facetValidatorsManager) {
        return new FacetEditorTab[]{new FacetConfigurationEditor(this)};
    }

    public void readExternal(Element element) throws InvalidDataException {
    }

    public void writeExternal(Element element) throws WriteExternalException {
    }

    public SCAFacetConfiguration.SCAFacetState getState() {
        SCAFacetState state = new SCAFacetState();
        Map<String, String> settings = state.getFacetSettings();

        settings.put(PROVIDER_KEY, selectedProvider.getId());

        return state;
    }

    public void loadState(SCAFacetConfiguration.SCAFacetState state) {
        Map<String, String> settings = state.getFacetSettings();

        if (settings.containsKey(PROVIDER_KEY)) {
            selectedProvider = SCAProviderRegistry.getInstance().getProviderById(settings.get(PROVIDER_KEY));
            if (selectedProvider == null) selectedProvider = VanillaSCAProvider.INSTANCE;
        }
    }

    public SCAProvider getSelectedProvider() {
        return selectedProvider;
    }

    public void setSelectedProvider(SCAProvider selectedProvider) {
        this.selectedProvider = selectedProvider;
    }

    public static class SCAFacetState {
        private Map<String, String> myFacetSettings = new HashMap<String, String>();

        @Property(surroundWithTag = false)
        @MapAnnotation(surroundKeyWithTag = false, surroundValueWithTag = false, surroundWithTag = false,
                entryTagName = "setting", keyAttributeName = "name", valueAttributeName = "value")
        public Map<String, String> getFacetSettings() {
            return myFacetSettings;
        }

        public void setFacetSettings(final Map<String, String> facetSettings) {
            myFacetSettings = facetSettings;
        }
    }
}
